import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login.vue'
import AdminHome from '../components/admin/AdminHome.vue'
import CommonMain from '../components/CommonMain.vue'
import Student from '../components/admin/Student.vue'
import Teacher from '../components/admin/Teacher.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    component: Login
  },
  {
    path: '/adminHome',
    component: AdminHome,
    redirect: '/commonMain',
    children: [
      { path:'/commonMain', component: CommonMain},
      { path:'/adminStudent', component: Student},
      { path:'/adminTeacher', component: Teacher},
    ],
  }
]

const router = new VueRouter({
  routes
})

// 挂载路由导航守卫
router.beforeEach((to,from,next)=>{
  // to：将要访问
  // from：从哪访问
  // next：接着干的事 next(url) 重定向到url上，next()继续访问to的路径
  if(to.path == '/login') return next();
  // 获取user
  const userFlag = window.sessionStorage.getItem('user'); // 取出当前用户
  if(!userFlag) return next('/login'); // 无值，返回登录页
  next(); //符合要求 放行

})


export default router
